package com.mtao.mapper;


import com.mtao.entity.SysMenu;
import com.mtao.entity.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: ems-admin-boot
 * @description: this is a interface
 * @author: starao
 * @create: 2021-11-27 14:18
 **/
@Mapper
public interface SysMenuMapper {

    /**
    * @Description: 获取菜单树
    * @Param: [roles]
    */
    @Select("select sys_role.id,sys_role.role_code,sys_role.role_name,sys_role_user.user_id " +
            "from sys_role Join sys_role_user on sys_role.id=sys_role_user.role_id " +
            "where user_id= #{userId};")
    List<SysMenu> getMenuTree();

    /**
    * @Description: 通过角色获取url
    * @Param: [roles]
    */
    @Select("select id,name,parent_id,path,type,sort,create_time,update_time,component,permission,icon,status,hidden from sys_menu")
    List<SysMenu> getMenuAll();

    /**
    * @Description: 获取权限列表
    * @Param: [roles]
    */
//    List<String> getPermission(@Param("roles") List<String> roles);

    /**
     * 通过role_id获取菜单列表
     * @param roleId
     * @return
     */
    @Select("select m.* from sys_role_menu as rm,sys_menu as m" +
            "where rm.menu_id=m.id and rm.role_id=#{roleId}")
    List<SysMenu> getRoleMenu(Integer roleId);

    /**
     * 通过user_id获取菜单列表
     * @param userId
     * @return
     */
    @Select("select m.* from sys_role_user as ru" +
            "left join sys_role as r on ru.role_id=r.id" +
            "left join sys_role_menu as rm on r.id=rm.role_id" +
            "left join sys_menu as m on rm.menu_id=m.id" +
            "where ru.user_id=#{userId}" +
            "order by m.sort asc")
    List<SysRole> getUserMenu(Integer userId);

    /**
     * 添加菜单
     * @param sysMenu
     */
    @Insert("insert into sys_menu (name,path,type,sort,component,permission,icon,parent_id,hidden,status) " +
            "values (#{name},#{path},#{type},#{sort},#{component},#{permission},#{icon},#{parentId},#{hidden},#{status})")
    void addMenu(SysMenu sysMenu);


    List<SysMenu> queryRoleMenu(List<Integer> rolesIds);

    /**
     * 删除菜单
     * @param id
     */
    @Delete("delete from sys_menu where id=#{id}")
    void delMenu(Integer id);

    /**
     * 编辑菜单
     * @param sysMenu
     */
    @Update("update sys_menu set name=#{name},path=#{path},type=#{type},sort=#{sort},component=#{component},permission=#{permission},icon=#{icon},parent_id=#{parentId},hidden=#{hidden},status=#{status} where id=#{id}")
    void editMenu(SysMenu sysMenu);
}
